package com.nian.app.service.api;

import com.nian.app.model.AppSwiperImages;
import com.nian.app.service.service.IAppSwiperImagesService;
import com.nian.common.model.Const;
import com.nian.common.model.ServerResponse;
import com.nian.images.api.ImagesAppAPI;
import com.nian.images.model.ImagesApp;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用途：
 * 开发：zhangmj
 * 日期：2020/4/2 0002 11:27
 */
@RestController
@RequestMapping("api")
public class AppSwiperImagesAPIImpl {

    @Autowired
    private IAppSwiperImagesService iAppSwiperImagesService;

    @Autowired
    private ImagesAppAPI imagesAppAPI;

    @GetMapping(value = "/swiper/list/{terminalType}/{pageType}")
    public ServerResponse<List<AppSwiperImages>> swiperList(@PathVariable("terminalType") Integer terminalType, @PathVariable("pageType") Integer pageType){
        List<AppSwiperImages> list = iAppSwiperImagesService.lambdaQuery().select(AppSwiperImages::getSwiperType, AppSwiperImages::getLinkType,
                AppSwiperImages::getLinkUrl, AppSwiperImages::getFileId, AppSwiperImages::getWeight)
                .eq(AppSwiperImages::getDeleteFlag, Const.DELETE_FLAG_UN)
                .eq(AppSwiperImages::getTerminalType, terminalType)
                .eq(AppSwiperImages::getPageType, pageType)
                .list();
        handlerImage(list);
        return ServerResponse.createBySuccess(list);
    }

    public void handlerImage(List<AppSwiperImages> list){
        if(CollectionUtils.isEmpty(list)){
            return;
        }
        String ids = list.stream().filter(mode-> mode.getFileId() != null).map(mode-> mode.getFileId().toString()).distinct().collect(Collectors.joining(","));
        List<ImagesApp> imagesList = imagesAppAPI.list(Const.INT_ZERO, ids);
        if(CollectionUtils.isEmpty(imagesList)){
            return;
        }
        for (AppSwiperImages swp:list) {
            for (ImagesApp images:imagesList) {
                if(Long.compare(swp.getFileId(), images.getImagesId()) == Const.INT_ZERO){
                    swp.setFileUrl(images.getImagesUrl());
                }
            }
        }
    }

}
